//
//  YDStockMarketTableHeaderView.m
//  PeiZiWangBei
//
//  Created by LK-IOS on 2018/1/19.
//  Copyright © 2018年 LK-IOS. All rights reserved.
//

#import "YDStockMarketTableHeaderView.h"
#import "YDBuyStrategyTitleButton.h"
@interface YDStockMarketTableHeaderView()


@property (nonatomic , weak) UIView *lineView;

//代码名称
@property (nonatomic , weak) UILabel *stockNameLabel;
//最新价按钮
@property (nonatomic , weak) YDBuyStrategyTitleButton *lastPriceButton;
//涨跌幅按钮
@property (nonatomic , weak) YDBuyStrategyTitleButton *upRateButton;


@end

@implementation YDStockMarketTableHeaderView


+ (instancetype)headerViewWithTableView:(UITableView *)tableView
{
    
    static NSString *stockMarketTableHeaderView = @"YDStockMarketTableHeaderView";
    YDStockMarketTableHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:stockMarketTableHeaderView];
    if (headerView == nil) {
        headerView = [[YDStockMarketTableHeaderView alloc] initWithReuseIdentifier:stockMarketTableHeaderView];
    }
    return headerView;
}


-(instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor whiteColor];
        [self lineView];
        
        [self stockNameLabel];
        [self lastPriceButton];
        [self upRateButton];
    }
    return self;
}


-(UILabel *)stockNameLabel
{
    if (_stockNameLabel == nil) {
        UILabel *stockNameLabel = [[UILabel alloc] init];
        [self.contentView addSubview:stockNameLabel];
        stockNameLabel.textColor = [UIColor colorWithHexString:@"687284"];
        stockNameLabel.font = [UIFont systemFontOfSize:font14ScreenWRate * LKScreenW];
        stockNameLabel.textAlignment = NSTextAlignmentCenter;
        stockNameLabel.text = @"股票名称";
        [stockNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.left.equalTo(self).offset(rate375(20));
        }];
        _stockNameLabel = stockNameLabel;
    }
    return _stockNameLabel;
}

-(YDBuyStrategyTitleButton *)lastPriceButton
{
    if (_lastPriceButton == nil) {
        YDBuyStrategyTitleButton *lastPriceButton = [YDBuyStrategyTitleButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:lastPriceButton];
        lastPriceButton.titleLabel.font = [UIFont systemFontOfSize:font14ScreenWRate * LKScreenW];
        lastPriceButton.titleLabel.textAlignment = NSTextAlignmentLeft;
        [lastPriceButton setTitleColor:[UIColor colorWithHexString:@"687284"] forState:UIControlStateNormal];
        [lastPriceButton setTitleColor:[UIColor colorWithHexString:@"687284"] forState:UIControlStateSelected];
        [lastPriceButton setTitle:@"最新价" forState:UIControlStateNormal];
        [lastPriceButton setImage:[UIImage imageNamed:@"norImageButton"] forState:UIControlStateNormal];
        [lastPriceButton setImage:[UIImage imageNamed:@"norImageButton"] forState:UIControlStateSelected];
        [lastPriceButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
        }];
        lastPriceButton.tag = 202;
        [lastPriceButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        _lastPriceButton = lastPriceButton;
    }
    return _lastPriceButton;
}



-(YDBuyStrategyTitleButton *)upRateButton
{
    if (_upRateButton == nil) {
        YDBuyStrategyTitleButton *upRateButton = [YDBuyStrategyTitleButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:upRateButton];
        upRateButton.titleLabel.font = [UIFont systemFontOfSize:font14ScreenWRate * LKScreenW];
        upRateButton.titleLabel.textAlignment = NSTextAlignmentLeft;
        [upRateButton setTitleColor:[UIColor colorWithHexString:@"687284"] forState:UIControlStateNormal];
        [upRateButton setTitleColor:[UIColor colorWithHexString:@"687284"] forState:UIControlStateSelected];
        [upRateButton setTitle:@"涨跌幅" forState:UIControlStateNormal];
        [upRateButton setImage:[UIImage imageNamed:@"zhangImageButton"] forState:UIControlStateNormal];
        [upRateButton setImage:[UIImage imageNamed:@"zhangImageButton"] forState:UIControlStateSelected];
        
        
        [upRateButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.right.equalTo(self).offset(-rate375(20));
        }];
        upRateButton.tag = 203;
        [upRateButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        _upRateButton = upRateButton;
    }
    return _upRateButton;
}


-(void)buttonClick:(YDBuyStrategyTitleButton *)button
{
    button.selected = !button.selected;
    if (button.tag == 202) {//价格
        self.lastPriceButton.selected = button.selected;
        self.upRateButton.selected = NO;
        [self.upRateButton setImage:[UIImage imageNamed:@"norImageButton"] forState:UIControlStateNormal];
        [self.upRateButton setImage:[UIImage imageNamed:@"norImageButton"] forState:UIControlStateSelected];
        if (self.lastPriceButton.selected) {
            [self.lastPriceButton setImage:[UIImage imageNamed:@"zhangImageButton"] forState:UIControlStateNormal];
            [self.lastPriceButton setImage:[UIImage imageNamed:@"zhangImageButton"] forState:UIControlStateSelected];
        }else{
            //dieSel
            [self.lastPriceButton setImage:[UIImage imageNamed:@"dieImageButton"] forState:UIControlStateNormal];
            [self.lastPriceButton setImage:[UIImage imageNamed:@"dieImageButton"] forState:UIControlStateSelected];
        }
        
        
    }else if (button.tag == 203){//涨跌幅
        self.upRateButton.selected = button.selected;
        self.lastPriceButton.selected = NO;
        [self.lastPriceButton setImage:[UIImage imageNamed:@"norImageButton"] forState:UIControlStateNormal];
        [self.lastPriceButton setImage:[UIImage imageNamed:@"norImageButton"] forState:UIControlStateSelected];
        if (self.upRateButton.selected) {
            [self.upRateButton setImage:[UIImage imageNamed:@"zhangImageButton"] forState:UIControlStateNormal];
            [self.upRateButton setImage:[UIImage imageNamed:@"zhangImageButton"] forState:UIControlStateSelected];
        }else{
            //dieSel
            [self.upRateButton setImage:[UIImage imageNamed:@"dieImageButton"] forState:UIControlStateNormal];
            [self.upRateButton setImage:[UIImage imageNamed:@"dieImageButton"] forState:UIControlStateSelected];
        }
        
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(stockMarketTableHeaderView:didClickButton:)]) {
        [self.delegate stockMarketTableHeaderView:self didClickButton:button];
    }
}






















-(UIView *)lineView
{
    if (_lineView == nil) {
        UIView *lineView = [[UIView alloc] init];
        [self.contentView addSubview:lineView];
        lineView.backgroundColor = [UIColor colorWithHexString:mainGray];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self);
            make.height.equalTo(@1);
        }];
        _lineView = lineView;
    }
    return _lineView;
}


@end
